const { parse } = require('@babel/parser');
const traverse = require('@babel/traverse').default;
const generate = require('@babel/generator').default;

const code = `
export default {
  computed: {
    oParamsT() { // 短语法函数定义
      return { age: 999 }
    }
  }
}
`;

// 解析代码为AST
const ast = parse(code, {
  sourceType: 'module',
  plugins: ['objectRestSpread']
});

let returnValue = null;

// 遍历AST查找computed.oParamsT
traverse(ast, {
  VariableDeclaration(path) {
    // 查找变量a的定义
    const aDeclaration = path.node.declarations.find(
      decl => decl.id.name === 'a'
    );
    
    if (aDeclaration && aDeclaration.init.type === 'ObjectExpression') {
      const aObject = aDeclaration.init;
      
      // 查找computed属性
      const computedProp = aObject.properties.find(
        prop => prop.key.name === 'computed'
      );
      
      if (computedProp && computedProp.value.type === 'ObjectExpression') {
        const computedObj = computedProp.value;
        
        // 查找oParamsT方法
        const oParamsTProp = computedObj.properties.find(
          prop => prop.key.name === 'oParamsT'
        );
        
        if (oParamsTProp) {
          // 处理ObjectMethod类型(简写方法)
          if (oParamsTProp.type === 'ObjectMethod') {
            const body = oParamsTProp.body;
            
            // 查找返回语句
            const returnStmt = body.body.find(
              stmt => stmt.type === 'ReturnStatement'
            );
            
            if (returnStmt) {
              returnValue = returnStmt.argument;
            }
          }
        }
      }
    }
  }
});

// 输出生成的代码
const result = returnValue 
  ? generate(returnValue).code 
  : '未找到返回值';

console.log('oParamsT的返回值:', result);
// 输出: { age: 999 }